package com.nowcoder.community.controller;

import com.nowcoder.community.dao.DiscussPostMapper;
import com.nowcoder.community.dao.UserMapper;
import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.Page;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.service.LikeService;
import com.nowcoder.community.service.UserService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName HomeController
 * @Description
 * @Author hhwu
 * @Date 2022/5/17 9:46
 * Version 1.0
 **/
@Controller
public class HomeController implements CommunityConstant {
    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private UserService userService;

    @Autowired
    private LikeService likeService;

    @RequestMapping(path = "/index", method = RequestMethod.GET)
    public String getIndexPage(Model model, Page page, @RequestParam(name = "orderMode", defaultValue = "0") int orderMode) {// 前端传过来的current会直接存入page的属性current中
        // 方法调用前，SpringMVC会自动实例化Model和Page，并将Page注入Model，所以，在thymeleaf中可以直接访问Page对象中的数据。，不用通过Model
        // 默认orderMode为0，即按照一般进行排序
        /**
         * @Author hhwu
         * @Description //TODO 返回帖子信息，每页10条
         * @Date 10:18 2022/5/19
         * @Param [model, page]
         * @return java.lang.String
         **/
        page.setRows(discussPostService.findDiscussPostRows(0));// 查询到的数据一共有多少条
        page.setPath("/index?orderMode=" + orderMode);// 某个页码的链接路径


        List<DiscussPost> list = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit(), orderMode);// 按照指定条件查询帖子
        List<Map<String, Object>> discussPosts = new ArrayList<>();
        if (list != null) {
            for (DiscussPost post :
                    list) {
                User user = userService.findUserById(post.getUserId());
                long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, post.getId());

                Map<String, Object> map = new HashMap<>();
                map.put("post", post);// 一条帖子的全部信息
                map.put("user", user);// 该条帖子对应的user的全部信息
                map.put("likeCount", likeCount);
                discussPosts.add(map);
            }
        }
        model.addAttribute("discussPosts", discussPosts);
        model.addAttribute("orderMode", orderMode);
        return "index";// 不需要写templates下的文件后缀
    }

    @RequestMapping(path = "/error", method = RequestMethod.GET)
    public String getErrorPage() {
        /**
         * @Author hhwu
         * @Description //TODO 跳转到500错误界面
         * @Date 21:41 2022/5/24
         * @Param []
         * @return java.lang.String
         **/
        return "/error/500";
    }

    // 拒绝访问时的提示页面
    @RequestMapping(path = "/denied", method = RequestMethod.GET)
    public String getDeniedPage() {
        /**
         * @Author hhwu
         * @Description //TODO 拒绝访问时的提示页面
         * @Date 16:45 2022/5/31
         * @Param []
         * @return java.lang.String
         **/
        return "/error/404";
    }
}
